package com.example.controller;

import com.example.entity.Result;
import com.example.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.ResponseEntity;

import java.util.List;
import java.util.Map;

@RequestMapping("/recommend")
@RestController
public class RecommendController {

    private RestTemplate restTemplate;

    @Autowired
    public void RecommendationController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @GetMapping
    public Result<List> getRecommendations() {

        //调用ThreadLocal线程
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");

        // 调用 Python 推荐服务
        String url = "http://localhost:5000/recommend/" + userId;

        // 发送 GET 请求
        ResponseEntity<List> response = restTemplate.getForEntity(url, List.class);

        // 返回推荐资源
        return Result.success(response.getBody());
    }

    @GetMapping("/related")
    public Result<List> getRelatedVideo(@RequestParam String title, @RequestParam String type){
        // 调用 Python 推荐服务
        String url = "http://localhost:5000/recommend_related?title=" + title + "&type=" + type;
        // 发送 GET 请求
        ResponseEntity<List> response = restTemplate.getForEntity(url, List.class);

        return Result.success(response.getBody());
    }

    @GetMapping("/schedule")
    public Result<List> getSchedule(@RequestParam String title){
        // 调用 Python 推荐服务
        String url = "http://localhost:5000/recommend?title=" + title;
        // 发送 GET 请求
        ResponseEntity<List> response = restTemplate.getForEntity(url, List.class);

        return Result.success(response.getBody());
    }
    @GetMapping("/learning")
    public Result<Object> getLearning(@RequestParam String schedule){
        // 调用 Python 推荐服务
        String url = "http://localhost:5000/learning?schedule=" + schedule;
        // 发送 GET 请求
        ResponseEntity<Object> response = restTemplate.getForEntity(url, Object.class);

        return Result.success(response.getBody());
    }

    @GetMapping("/learning/{id}")
    public Result<Object> getLearning(@PathVariable Integer id){
        // 调用 Python 推荐服务
        String url = "http://localhost:5000/learning/" + id;
        // 发送 GET 请求
        ResponseEntity<Object> response = restTemplate.getForEntity(url, Object.class);

        return Result.success(response.getBody());
    }

    @GetMapping("/directory/{id}")
    public Result<Object> getContents(@PathVariable Integer id){
        System.out.println(id);
        // 调用 Python 推荐服务
        String url = "http://localhost:5000/directory/" + id;
        // 发送 GET 请求
        ResponseEntity<List> response = restTemplate.getForEntity(url, List.class);

        return Result.success(response.getBody());
    }

    @GetMapping("/random/{id}")
    public Result<Object> getRandomCourse(@PathVariable Integer id){
        System.out.println(id);
        // 调用 Python 推荐服务
        String url = "http://localhost:5000/random/" + id;
        // 发送 GET 请求
        ResponseEntity<List> response = restTemplate.getForEntity(url, List.class);

        return Result.success(response.getBody());
    }

    @PostMapping("/chat")
    public Result<Object> chat(@RequestBody Map<String, Object> request){
        String url = "http://localhost:5000/chat";
        ResponseEntity<String> response = restTemplate.postForEntity(url,request,String.class);
        return Result.success(response.getBody());
    }

    @GetMapping("/nodes")
    public Result<Object> getNodes(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "") String search){
        // 调用 Python 推荐服务
        String url = "http://localhost:5000/nodes?page="+page+"&search="+search;
        // 发送 GET 请求
        ResponseEntity<Object> response = restTemplate.getForEntity(url, Object.class);
        return Result.success(response.getBody());
    }
}
